package Offer_30;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2023/3/28 11:31
 */
public class MinStack {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(512);
        minStack.push(-1024);
        minStack.push(-1024);
        minStack.push(512);

        minStack.pop();
        minStack.min();
        minStack.pop();
        minStack.min();
        minStack.pop();
        minStack.min();
    }
    Deque<Integer> stack;
    Deque<Integer> minStack;
        /** initialize your data structure here. */
        public MinStack() {
           stack  = new LinkedList<>();
           minStack  =  new LinkedList<>();
        }

        public void push(int x) {
           if (minStack.isEmpty()){
               minStack.push(x);
           }else if(x <= minStack.peek()) {
               minStack.push(x);
            }
           stack.push(x);
        }

        public void pop() {
            if (stack.isEmpty()){
                return;
            }
            if (stack.peek().equals(minStack.peek())){
                minStack.pop();
            }
            stack.pop();
        }

        public int top() {
            if (stack.isEmpty()){
                return -1;
            }
            return stack.peek();
        }

        public int min() {
            if (minStack.isEmpty()){
                return -1;
            }
            return minStack.peek();
        }
    }

